package userModuleApi

import (
	"errors"
	"golang.org/x/crypto/bcrypt"
)

type UserModuleApi struct{}

type UserRequest struct {
	UserEmail string `json:"userEmail" binding:"required"`
	Password  string `json:"password" binding:"required"`
}

// HashPwd 加密哈希密码
func HashPwd(pwd string) (string, error) {
	hash, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost)
	if err != nil {
		return "", err
	}
	return string(hash), nil
}

func CheckPassword(hashPwd, pwd string) (bool, error) {
	err := bcrypt.CompareHashAndPassword([]byte(hashPwd), []byte(pwd))
	if err != nil {
		if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) {
			return false, nil
		}
		return false, err
	}
	return true, nil
}
